1875B - Jellyfish and Game - CodeForces Solution


brute force greedy implementation

Please click on ads to support us..

Python Code:

n = int(input())

for _ in range(n):

    [j, m, k] = list(map(int, input().split()))

    odd = list(map(int, input().split()))
    a, b = max(odd), min(odd)
    even = list(map(int, input().split()))
    c, d = max(even), min(even)

    if b < c:
        if (k-1)%2 == 0:
            ans = sum(odd) - b + c
        else:
            ans = sum(odd) - b + c - max(a, c) + min(b, d)
    else:
        if (k-1)%2 == 0:
            ans = sum(odd)
        else:
            ans = sum(odd) - a + d
    print(ans)

C++ Code:

#include<bits/stdc++.h>
#define endl '\n'

using namespace std;

const int MAXN=55;
const int MAXM=55;

int n,m,k;
int a[MAXN];
int b[MAXM];

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);

    int t;
    cin>>t;
    while(t--)
    {
        cin>>n>>m>>k;
        for(int i=0; i<n; i++) cin>>a[i];
        for(int i=0; i<m; i++) cin>>b[i];

        sort(a,a+n);
        sort(b,b+m);

        long long sA=0;
        long long sB=0;
        for(int i=0; i<n; i++) sA+=a[i];
        for(int i=0; i<m; i++) sB+=b[i];

        if(n==1)
        {
            if(a[0]<=b[m-1])
            {
                if(k%2) cout<<b[m-1]<<endl;
                else cout<<min(a[0],b[0])<<endl;
            }
            else
            {
                if(k%2) cout<<a[0]<<endl;
                else cout<<b[0]<<endl;
            }
            continue;
        }

        if(m==1)
        {
            if(b[0]>a[n-1])
            {
                if(k%2) cout<<sA+b[0]-a[0]<<endl;
                else cout<<sA<<endl;
            }
            else if(b[0]<a[0])
            {
                if(k%2) cout<<sA<<endl;
                else cout<<sA-a[n-1]+b[0]<<endl;
            }
            else
            {
                sA-=a[0];
                sA+=b[0];
                if(k%2) cout<<sA<<endl;
                else cout<<sA+a[0]-a[n-1]<<endl;
            }
            continue;
        }

        int mina=2e9,maxa=0;
        int minb=2e9,maxb=0;
        for(int i=0; i<n; i++)
        {
            mina=min(mina, a[i]);
            maxa=max(maxa, a[i]);
        }
        for(int i=0; i<m; i++)
        {
            minb=min(minb, b[i]);
            maxb=max(maxb, b[i]);
        }

        if(mina<=minb && maxb<=maxa)
        {
            sA+=maxb;
            sA-=mina;
            if(k%2) cout<<sA<<endl;
            else cout<<sA-maxa+mina<<endl;
        }
        else if(mina<=minb && maxa<maxb)
        {
            if(k%2) cout<<sA+maxb-mina<<endl;
            else cout<<sA<<endl;
        }
        else if(minb<mina && maxb<=maxa)
        {
            if(maxb>mina)
            {
                sA+=maxb;
                sA-=mina;
            }
            if(k%2) cout<<sA<<endl;
            else cout<<sA-maxa+minb<<endl;
        }
        else if(minb<mina && maxa<maxb)
        {
            sA+=maxb;
            sA-=mina;
            if(k%2) cout<<sA<<endl;
            else cout<<sA-maxb+minb<<endl;
        }
    }

    return 0;
}


Comments

Submit
0 Comments
More Questions

Numbers in a matrix
Sequences
Split houses
Divisible
Three primes
Coprimes
Cost of balloons
One String No Trouble
Help Jarvis!
Lift queries
Goki and his breakup
Ali and Helping innocent people
Book of Potion making
Duration
Birthday Party
e-maze-in
Bricks Game
Char Sum
Two Strings
Anagrams
Prime Number
Lexical Sorting Reloaded
1514A - Perfectly Imperfect Array
580A- Kefa and First Steps
1472B- Fair Division
996A - Hit the Lottery
MSNSADM1 Football
MATCHES Playing with Matches
HRDSEQ Hard Sequence
DRCHEF Doctor Chef